package org.example.conter;

import io.vertx.core.buffer.Buffer;
import org.example.CommonMsg;



public class CounterPacket {
    public static final int START_BODY_LENGTH = 0;
    public static final int START_CHECKSUM = 4;
    public static final int START_MSG_SRC = 5;
    public static final int START_MSG_DST = 7;
    public static final int START_MSG_TYPE = 9;
    public static final int START_STATUS = 11;
    public static final int START_PACKET_NO = 12;

    public static final int HEADER_LENGTH = START_PACKET_NO + 8;

    // BODY_LENGTH=5
    // 1 1 1 1 1
    //BYTE
    // 5 1 2 3 4 5 6 1 1 1 1 1 1 1 1

    // BODY_LENGTH, CHECK_SUM
    public static CommonMsg readCommonMsg(Buffer buffer) {
        CommonMsg msg = new CommonMsg();
        msg.setBodyLength(buffer.getInt(START_BODY_LENGTH));
        msg.setChecksum(buffer.getByte(START_CHECKSUM));
        msg.setMsgSrc(buffer.getShort(START_MSG_SRC));
        msg.setMsgDst(buffer.getShort(START_MSG_DST));
        msg.setMsgType(buffer.getShort(START_MSG_TYPE));
        msg.setStatus(buffer.getByte(START_STATUS));
        msg.setMsgNo(buffer.getLong(START_PACKET_NO));
        return msg;
    }

    public static Buffer writeToBuffer(CommonMsg msg) {
        return Buffer.buffer().appendInt(msg.getBodyLength())
                .appendByte(msg.getChecksum())
                .appendShort(msg.getMsgSrc())
                .appendShort(msg.getMsgDst())
                .appendShort(msg.getMsgType())
                .appendByte(msg.getStatus())
                .appendLong(msg.getMsgNo())
                .appendBytes(msg.getBody());
    }





}
